<template>
    <div id="roleModify" class="functionalArea">
        <div>
            <el-form class="formEmbedTable" :model="detailData">
                <table class="formEmbedTable"> 
                    <tr>
                        <td>Transaction Sequence No.</td>
                        <td>{{detailData.flowNo}}</td>	
                    </tr>
                    <tr>
                        <td>Submit</td>
                        <td>{{detailData.userAlias}}</td>	
                    </tr>
                </table>
            </el-form>
        </div>
        <div >
            <el-form ref="detailData" :model="detailData" class="formEmbedTable mt20">
                <table>
                    <caption>Role Information</caption>
                    <tr>
                        <td>Role Name</td>
                        <td>{{detailData.roleName}}</td>
                        <td>Category</td>
                        <td>{{$tools.dict.BranchLevel(detailData.roleType)}}</td>					
                    </tr>
                    <tr>
                        <td>Description</td>
                        <td >{{detailData.roleDesc}}</td>
                        <td>Action</td>
                        <td>{{detailData.actionType==0?"Add":detailData.actionType==1 ? "Modify" : "Delete"}}</td>
                    </tr>
                </table>
            </el-form>  
        </div>
        <div class="mt20"><RoleTree :menuTree="menuTree"></RoleTree></div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" @click="backMain">Back</el-button>
        </div>
    </div>
</template>
<script>
import ghbIcon from "@src/components/icon";
import RoleTree from "./editTree";
export default {
  name: "roleModify",
  components: {
    ghbIcon,
    RoleTree
  },
  props: ["detailData",],
  mounted() {
      let roleBsn = this.detailData.imBsnList;
      let bsnTemp1 = []; //修改后菜单集合
      //格式化菜单
      $.each(roleBsn, function(i, m) {
        let bsnTemp = {};
        $.each(m, function(j, n) {
          bsnTemp[j] = n.value;
        });
        bsnTemp1.childrens = [];
        bsnTemp1.push(bsnTemp);
      });
      //console.log("bsnTemp1:" + JSON.stringify(bsnTemp1));
      // 封装树结构（新增的1,2,3级菜单）
      let bsnTemp3 = []; //一级菜单树
      $.each(bsnTemp1, function(i, n) {
        if (n.bsnCode.length == 4) {
          n.childrens = [];
          bsnTemp3.push(n);
          bsnTemp1.splice(i, 1);
        }
      });
      //console.log("bsnTemp3:" + JSON.stringify(bsnTemp3));
      //console.log("bsnTemp1:" + JSON.stringify(bsnTemp1));
      let arr=[]
      $.each(bsnTemp3,function(i,m){//耳机菜单
        $.each(bsnTemp1,function(j,n){
          if(n.bsnCode.length==6 && (n.bsnCode).slice(0,4)==m.bsnCode){
            n.childrens=[]
            n.newMenuFlag = n.menuFlag;
            n.newMenuAuth = n.menuAuth;
            bsnTemp3[i].childrens.push(n);
            arr.push(j)
          }
        })
      })
      //三级菜单
      $.each(bsnTemp3,function(i,m){
        $.each(m,function(j,n){
          $.each(bsnTemp1,function(k,p){
            if(p.bsnCode.length==8 && (p.bsnCode).slice(0,6)==n.bsnCode){
              p.newMenuFlag=p.menuFlag;
              p.newMenuAuth=p.menuAuth;
              bsnTemp3[i].childrens[j].childrens.push(p);
              arr.push(k)
            }
          })
        })
      })
      //判断剩余的数据

      let bsnTemp2=[]
      //console.log("arr:" + JSON.stringify(arr));
      $.each(bsnTemp1,function(i,m){
        if($.inArray(i,arr)==-1){
          bsnTemp2.push(m);
        }
      })
      //console.log("bsnTemp2:" + JSON.stringify(bsnTemp2));
      if(bsnTemp2.length!=0){//还有剩余2/3级的菜单
        //console.log(bsnTemp2.length)
        $.each(bsnTemp2,function(i,m){
          //console.log("m:"+JSON.stringify(m))
          if(m.bsnCode.length==6){//二级菜单
            let temp={}
            m.childrens=[]
            m.newMenuFlag=m.menuFlag;
            m.newMenuAuth=m.menuAuth;
            temp.bsnCode=m.bsnCode.substring(0,4)
            temp.childrens=[];
            temp.childrens.push(m)
            console.log(JSON.stringify(temp))
            
            //判断一级菜单是否存在
            let index=-1
            $.each(bsnTemp3,function(i,n){
              if(n.bsnCode==temp.bsnCode){
                index=i;
                //break;
              }
            })
            console.log("index:"+index)
            if(index==-1){//不存在

              bsnTemp3.push(temp)
            }
            if(index!=-1){
              bsnTemp3[index].childrens.push(m);
            }
          }
          //三级菜单
          if(m.bsnCode.length==8){
            
            m.newMenuFlag=m.menuFlag;
            m.newMenuAuth=m.menuAuth;
            let firstBsn=m.bsnCode.substring(0,4)
            let secBsn=m.bsnCode.substring(0,6)
            let firstIndex=-1;
            let secIndex=-1;
            $.each(bsnTemp3,function(i,n){
              if(n.bsnCode==firstBsn){//一级菜单存在
                firstIndex=i;
                //二级菜单
                $.each(n,function(j,p){
                  
                  if(p.bsnCode==secBsn){//二级菜单存在
                    secIndex=j;
                    bsnTemp3[i].childrens[j].childrens.push(m)
                  }
                })
              }
            })
            if(firstIndex==-1){//一级菜单不存在,二级菜单一定不存在
              let temp={};
              let secTemp={}
              temp.bsnCode=firstBsn;
              temp.childrens=[]
              secTemp.bsnCode=secBsn;
              secTemp.childrens=[]
              secTemp.childrens.push(m)
              temp.childrens.push(secTemp)
              bsnTemp3.push(temp)
            }
            if(firstIndex!=-1 && secIndex==-1){//一级菜单存在,二级菜单不存在
              let secTemp={};
              secTemp.bsnCode=secBsn;
              secTemp.childrens=[];
              secTemp.childrens.push(m)
              bsnTemp3[firstIndex].childrens.push(secTemp)
            }
          }
        })
      }
      //console.log("bsnTemp3:" + JSON.stringify(bsnTemp3));
      this.menuTree = bsnTemp3;
      //console.log("bsnTemp3:" + JSON.stringify(bsnTemp3));

  },
  data() {
    return {
      menuTree: [], //菜单树
      roleInfoList: [], //未用到
      roleInfo: {}, //修改后角色信息
      oldRole: {}, //修改前角色信息
      rejectForm: {
        //审核拒绝列表
        actionFlag: "",
        bsnAuthList: [],
        authFlag: "1",
        rejReason: ""
      },
      agreeForm: {
        actionFlag: "",
        bsnAuthList: [],
        authFlag: "0"
      },
      dialogTableVisible: false
    };
  },
  methods: {
    backMain() {
      this.$emit("back");
    },
    
  }
};
</script>
